//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include <wx/statline.h>
#include "../common/MarkupSTL.h"
#include "wirelessstatpanel.h"

CWirelessStatPanel::CWirelessStatPanel(wxWindow* parent):
	wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
	 m_nFlags(wxALL|wxGROW), m_nBorder(3)
{
	wxBoxSizer* pGlobal = new wxBoxSizer(wxVERTICAL);

	m_pSizer = new wxFlexGridSizer(3, 4, 0, 0);

	SetMinSize(wxSize(200,200));

	pGlobal->Add(m_pSizer, 1 );

	SetSizer(pGlobal);
	pGlobal->FitInside(this);
	SetScrollRate(5,5);
}

CWirelessStatPanel::~CWirelessStatPanel(void)
{
}

double DbmToWatt(double nVal)
{
	if (!nVal) return 1;

	return pow(10, nVal/10);
}

void CWirelessStatPanel::ShowStats(const RTSTRING& strData)
{
	m_bShow  = false;

	CMarkupSTL	doc;

	if ( doc.SetDoc(strData.c_str()) )
	{
		if ( doc.FindChildElem(WRLS_XML_TAG_STATUS) )
		{
			std::string strTmp = doc.GetChildAttrib(WRLS_XML_ATTR_ISUP);

			if ( strTmp=="1" || strTmp=="true" )
			{
				RenderItem( doc, "SSID", _("SSID") );
				RenderItem( doc, "BSSID", _("BSSID") );
				RenderItem( doc, "Mode", _("Mode") );
				RenderItem( doc, "Channel", _("Channel") );
				RenderItem( doc, "Supported_Rates", _("Supported Rates") );
				RenderItem( doc, WRLS_XML_ATTR_RATE, _("Rate") );
				RenderProgressItem( doc, WRLS_XML_ATTR_NOISE,  _("Noise"), -100, -60, DbmToWatt  );
			}
		}
	}

	Show(m_bShow);
}

#define DEF_MULT 100000.0

void CWirelessStatPanel::RenderProgressItem( class CMarkupSTL& doc, const char* szATTRNAME, const wxString& strLabel, int nMin, int nMax, VALCONVERT fn)
{
	std::string strVal = doc.GetChildAttrib(szATTRNAME);

	CONTROLS::iterator i = m_controls.find(szATTRNAME);

	if ( !strVal.empty() )
	{
		m_bShow = true;
	}

	wxString strValue = wxString::FromAscii( strVal.c_str() );

	double dMax = DEF_MULT*fn(nMax);
	double dMin = DEF_MULT*fn(nMin);
	int nMaxRange = (dMax-dMin);

	double dVal = DEF_MULT*fn( atol(strVal.c_str()) );
	int nDist  = (dVal-dMin);

	int nVal  = (100*(dVal-dMin))/(dMax-dMin);

	if ( i==m_controls.end() )
	{
		wxStaticText* pLabel = new wxStaticText(this, wxID_ANY, strLabel+wxT(":"));
		m_pSizer->Add(pLabel, 0, wxALIGN_BOTTOM|m_nFlags, m_nBorder);


		wxGauge* p = new wxGauge(this, wxID_ANY, nMaxRange, wxDefaultPosition, wxSize(40, 20), wxGA_HORIZONTAL|wxGA_SMOOTH );
		m_pSizer->Add(p, 0, wxGROW|wxALIGN_CENTER_VERTICAL|m_nFlags, m_nBorder);

		p->SetValue(nVal);

		CONTROLLIST lst;
		lst.push_back(p);

		m_controls[szATTRNAME] = lst;
	}
	else
	{
		CONTROLLIST& lst = i->second;
		((wxGauge*)lst[0])->SetValue( nVal );
	}
}

void CWirelessStatPanel::RenderItem( class CMarkupSTL& doc, const char* szATTRNAME, const wxString& strLabel )
{
	std::string strVal = doc.GetChildAttrib(szATTRNAME);

	CONTROLS::iterator i = m_controls.find(szATTRNAME);

	if ( !strVal.empty() )
	{
		m_bShow = true;
	}

	wxString strValue = wxString::FromAscii( strVal.c_str() );

	if ( i==m_controls.end() )
	{
		wxStaticText* pLabel = new wxStaticText(this, wxID_ANY, strLabel+wxT(":"));
		m_pSizer->Add(pLabel, 0, wxALIGN_BOTTOM|m_nFlags, m_nBorder);

		wxTextCtrl* p = new wxTextCtrl(this, wxID_ANY, strValue, wxDefaultPosition, wxDefaultSize, wxTE_READONLY);
		m_pSizer->Add(p, 0, wxALIGN_CENTER_VERTICAL|m_nFlags, m_nBorder);
		
		CONTROLLIST lst;
		lst.push_back(p);

		m_controls[szATTRNAME] = lst;
	}
	else
	{
		CONTROLLIST& lst = i->second;
		((wxTextCtrl*)lst[0])->SetValue( strValue );
	}
}